<template>
  <section class="content">
    <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">入库管理</h3>
      </div>
      <div class="form-horizontal">
        <div class="box-body">
          <div class="clearfix row">
            <div class="col-sm-12 flex" style="align-items: initial">
              <label class="control-label bg-gray min-width">仓库位置：</label>
              <div class="inline-block">
                <div class="m-b-20" v-if="userPrivilegeStock.length == 0">
                  <label class="payment">
                    <i class="fa fa-warning"></i>
                    您当前还未添加仓库
                    <router-link to="/v1/goods/category/getList" class="text-red text-bold">【立即添加】</router-link>
                  </label>
                </div>
                <div v-else>
                  <button type="button" class="btn btn-default m-b-20 btn-flat m-r-10 pull-left"
                          @click="chooseTransferWh(-1,null)"
                          :class='[{ "btn-info" :activeInStorageWh == -1}]'>全部
                  </button>
                  <button v-for="(inStorageWhItem, index) in userPrivilegeStock" type="button"
                          class="btn btn-default btn-flat m-r-10 m-b-20"
                          :class='[{ "btn-info" :index==activeInStorageWh}]'
                          @click="chooseTransferWh(index,inStorageWhItem.id)">{{inStorageWhItem.name}}
                  </button>
                </div>
              </div>
            </div>
          </div>
          <div class="clearfix row">
            <div class="col-sm-12 flex" style="align-items: initial">
              <label class="control-label bg-gray min-width">入库状态：</label>
              <div class="inline-block">
                <button v-for="(inStorageStateItem, index) in inStorageState" type="button"
                        class="btn btn-default btn-flat m-r-10 m-b-20"
                        :class='[{ "btn-info" :index==activeInStorageState}]'
                        @click="chooseTransferStatus(index,inStorageStateItem.status)">{{inStorageStateItem.name}}
                </button>
              </div>
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-12 flex">
              <label class="control-label min-width">输入搜索：</label>
              <div class="inline-block">
                <input style="width: 387.5px;" type="text"
                       class="form-control pull-left" placeholder="请输入入库单编号" v-model="getInStorageListParam.inNum">
                <button class="btn bg-green pull-left m-lr-10" v-if="$_has('sys:whin:list')"
                        @click="searchAllocationNum">搜索
                </button>
                <button class="btn btn-danger pull-left m-lr-10" @click="resetCondition">重置</button>
              </div>
            </div>
          </div>
        </div>
        <!-- /.box-body -->

        <!-- /.box-footer -->
      </div>
    </div>
    <div class="row">
      <div class="col-xs-12">
        <div class="box box-info">
          <div class="box-header with-border">
            <h3 class="box-title">数据列表</h3>
            <!--<button type="button" class="btn btn-success pull-right m-r-10" v-if="$_has('sys:whin:in')">导入</button>-->
            <!--<button type="button" class="btn btn-success pull-right m-r-10" v-if="$_has('sys:whin:out')">导出</button>-->
            <button type="button" class="btn btn-primary pull-right m-r-10" data-toggle="modal"
                    data-target="#modal-default2" @click="addInStorck" v-if="$_has('sys:whin:add')">创建入库单
            </button>
            <!--<button type="button" class="btn btn-success pull-right m-r-10" data-toggle="modal"-->
            <!--data-target="#modal-default" v-if="$_has('sys:whin:fast')">快速入库-->
            <!--</button>-->
          </div>
          <!-- /.box-header -->
          <div class="box-body">
            <table id="example3" class="table table-bordered table-hover">
              <div class="overlay" v-if="show">
                <i class="fa fa-refresh fa-spin"></i>
              </div>
              <thead>
              <tr>
                <!--<th>-->
                <!--<label>-->
                <!--<input type="checkbox" v-model='checked'>-->
                <!--</label>-->
                <!--</th>-->
                <th>入库单编号</th>
                <th>仓库</th>
                <th>入库类型</th>
                <th style="width: 40%;">入库依据</th>
                <th>状态</th>
                <th style="width: 20%;">操作</th>
              </tr>
              </thead>
              <tbody>
              <tr v-if="getInStorageList.length == 0">
                <td colspan="6">
                  没有数据
                </td>
              </tr>
              <tr v-else v-for="(getInStorageListItem,index) in getInStorageList" :key="index">
                <!--<td>-->
                <!--<label>-->
                <!--<input type="checkbox" v-model='checkList'-->
                <!--:value="getInStorageListItem.id">-->
                <!--</label>-->
                <!--</td>-->
                <td>{{getInStorageListItem.inNum}}</td>
                <td>{{getInStorageListItem.whName}}</td>
                <td v-for="(inType,i) in analysisType.inStorck" :key="i"
                    v-if="inType.id == getInStorageListItem.inType">{{inType.name}}
                </td>
                <td>{{getInStorageListItem.inVoucher}}</td>
                <td
                  :class='[{ "text-red" : getInStorageListItem.status == 1},{ "text-green" : getInStorageListItem.status != 1}]'>
                  {{inStorageState[getInStorageListItem.status].name}}
                </td>
                <td>
                  <div class="flex">
                    <span style="flex: 1">
                      <a v-if="getInStorageListItem.status == 1 && $_has('sys:whin:okin')" class="text-green m-lr-10"
                         data-toggle="modal"
                         data-target="#modal-default3" @click="acknowledgementFn(getInStorageListItem.inNum)">确认入库</a>
                    </span>
                    <span style="flex: 1">
                      <a
                        v-if="getInStorageListItem.inType!=2 && getInStorageListItem.status == 1 && $_has('sys:whin:edit')"
                        @click="editorInStorage(getInStorageListItem.inNum)"
                        data-toggle="modal" data-target="#modal-default2" class="text-green m-lr-10">编辑</a>
                    </span>
                    <span style="flex: 1">
                      <a @click="outCancel(getInStorageListItem.inNum)" class="text-green"
                         v-if="getInStorageListItem.inType!=2 && getInStorageListItem.status == 1 && $_has('sys:whin:del')">取消</a>
                    </span>
                    <span style="flex: 1">
                      <a class="text-green m-lr-10" data-toggle="modal" data-target="#modal-default2"
                         @click="detailInStorage(getInStorageListItem.inNum,getInStorageListItem.status)"
                         v-if="$_has('sys:whin:detail')">详情</a>
                    </span>
                  </div>
                </td>
              </tr>
              </tbody>
            </table>
            <div class="clearfix" v-if="getInStorageList.length != 0">
              <div class="m-t-15 pull-right">
                <pagination :total="pages.inStorckList.total" :current-page='pages.inStorckList.pageNum'
                            :display="pages.inStorckList.display" @pagechange="pagechange"></pagination>
              </div>
            </div>
          </div>
          <!-- /.box-body -->
        </div>
        <!-- /.box -->

      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
    <!--弹出框位置-->
    <div class="modal fade" id="modal-default">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">快速入库</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal max_height">
              <div class="clearfix">
                <div class="form-group">
                  <div class="col-sm-6">
                    <label class="control-label">入库单编号：</label>
                    <div class="form-control">
                      IS0233185003
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <label class="control-label">入库类型：</label>
                    <div class="">
                      <select class="form-control">
                        <option value="请选择仓库位置">请选择入库类型</option>
                      </select>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <label class="control-label">仓库位置：</label>
                    <div class="">
                      <select class="form-control">
                        <option value="请选择仓库位置">请选择仓库位置</option>
                      </select>
                    </div>
                  </div>
                  <div class="col-sm-6">
                    <label class="control-label">货架位：</label>
                    <div class="">
                      <select class="form-control">
                        <option value="请选择仓库位置">请选择仓库位置</option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="clearfix">
                  <div class="col-sm-12">
                    <table id="example4" class="table table-bordered table-hover marginNonde">
                      <thead>
                      <tr>
                        <th>商品图片</th>
                        <th>商品信息</th>
                        <th>库存</th>
                        <!--<th>调拨数量</th>-->
                        <th>入库数量</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr>
                        <td>
                          <img src="@/assets/dist/img/user2-160x160.jpg" alt="">
                        </td>
                        <td>
                          <label class="">
                            <p>
                              <label>商品编码：</label>
                              <span>000201010</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>000201010</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>000201010</span>
                            </p>
                          </label>
                        </td>
                        <td>33</td>
                        <td>
                          <input type="text" class="form-control" style="width: 120px;display: inline-block;">
                        </td>
                      </tr>
                      <tr>
                        <td>
                          <img src="@/assets/dist/img/user2-160x160.jpg" alt="">
                        </td>
                        <td>
                          <label class="">
                            <p>
                              <label>商品编码：</label>
                              <span>000201010</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>000201010</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>000201010</span>
                            </p>
                          </label>
                        </td>
                        <td>33</td>
                        <td>
                          <input type="text" class="form-control" style="width: 120px;display: inline-block;">
                        </td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal">保存</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>

    <div class="modal fade" id="modal-default2">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header bg-info">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">{{modalInfo}}</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal">
              <div class="clearfix">
                <div class="form-group">
                  <div class="col-sm-4 flex" v-if="editorGetInStorage.whInVo.inNum">
                    <label class="control-label bg-gray min-width">入库单编号：</label>
                    <label v-if="editorGetInStorage.whInVo.inNum" class="control-label min-width">{{editorGetInStorage.whInVo.inNum}}</label>
                  </div>
                  <div class="col-sm-4 flex">
                    <label class="min-width bg-gray control-label"><sup class="text-red">*</sup>入库类型：</label>
                    <div class="inline-block">
                      <select name="" :disabled="!isReadonly" v-model="editorGetInStorage.whInVo.inType"
                              class="form-control">
                        <option value="">请选择入库类型</option>
                        <option v-for="analysisTypeInStorck in analysisType.inStorck" :value="analysisTypeInStorck.id">
                          {{analysisTypeInStorck.name}}
                        </option>
                      </select>
                    </div>
                  </div>
                  <div class="col-sm-4 flex">
                    <label class="min-width bg-gray control-label"><sup class="text-red">*</sup>仓库：</label>
                    <div class="inline-block">
                      <div v-if="userPrivilegeStock.length == 0">
                        <label class="payment">
                          <i class="fa fa-warning"></i>
                          请添加仓库
                          <router-link to="/v1/goods/category/getList" class="text-red text-bold">【立即添加】</router-link>
                        </label>
                      </div>
                      <select v-else name="" :disabled="!isReadonly" v-model="editorGetInStorage.whInVo.whId"
                              class="form-control"
                              @change="chooseWarehouse(editorGetInStorage.whInVo.whId)">
                        <option value="">请选择仓库</option>
                        <option v-for="userPrivilegeStockItem in userPrivilegeStock" :value="userPrivilegeStockItem.id">
                          {{userPrivilegeStockItem.name}}
                        </option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="form-group" v-show="isReadonly">
                  <label class="col-sm-12">
                    <a v-if="editorGetInStorage.whInVo.whId" class="btn btn-primary pull-left m-r-10"
                       data-toggle="modal"
                       data-target="#modal-default4" @click="addProduct()">添加产品</a>
                    <a v-else class="btn btn-primary pull-left m-r-10"
                       @click="addProduct()">添加产品</a>
                  </label>
                </div>
                <div class="clearfix" v-if="editorGetInStorage.whInListVOS.length != 0">
                  <div class="">
                    <table class="table table-bordered table-hover marginNonde">
                      <thead>
                      <tr>
                        <th style="width: 13%;">商品图片</th>
                        <th>商品信息</th>
                        <th v-if="showShelf">货架</th>
                        <th>库存</th>
                        <th style="width: 10%;">入库数量</th>
                        <th v-show="isReadonly">操作</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr v-if="editorGetInStorage.whInListVOS.length == 0">
                        <td colspan="6">没有数据</td>
                      </tr>
                      <tr class="text-left" v-for="(editorGetInStorageItem,index) in editorGetInStorage.whInListVOS">
                        <td>
                          <img v-if="editorGetInStorageItem.goodPic" :src="editorGetInStorageItem.goodPic" alt="">
                          <img v-else src="@/assets/noPic.png" alt="">
                        </td>
                        <td class="text-left" style="padding-left: 20px;">
                          <label>
                            <p>
                              <label>商品编码：</label>
                              <span>{{editorGetInStorageItem.sku}}</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>{{editorGetInStorageItem.goodName}}</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>{{editorGetInStorageItem.brandName}}</span>
                            </p>
                          </label>
                        </td>
                        <td v-if="showShelf">
                          <div class="pull-left">
                            <ul style="margin: 0;">
                              <li style="margin: 3px 0;" v-for=" whPositionInfo in editorGetInStorageItem.stockVOs">
                                <div class="input-group">
                                  <select name="" style="width: 100px" class="form-control"
                                          v-model="whPositionInfo.stockNo" :disabled="!isReadonly">
                                    <option>
                                      {{whPositionInfo.stockNo}}
                                    </option>
                                  </select>
                                  <span class="input-group-addon curren"><i class="fa fa-remove"></i></span>
                                  <i class="fa fa-plus pull-right"></i>
                                </div>
                              </li>
                            </ul>
                          </div>
                          <div class="pull-right">

                          </div>
                        </td>
                        <td>{{editorGetInStorageItem.countStock}}</td>
                        <td>
                          <p class="p-tb-8" v-for="(stockItem, stockIndex) in editorGetInStorageItem.stockVOs"
                             :key="stockIndex">
                            <input type="text" class="form-control text-center" @change="maxSendQty"
                                   style="width: 120px;display: inline-block;" v-model="stockItem.sendQty"
                                   :disabled="!isReadonly">
                          </p>
                        </td>
                        <td v-show="isReadonly">
                          <a href="javascript:;" class="text-green" @click="deleteInStorck(index)">删除</a>
                        </td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer text-center">
            <button v-if="editorGetInStorage.whInListVOS.length != 0 && editorGetInStorage.whInVo.whId" type="button"
                    class="btn btn-primary"
                    @click="submitInStorck" v-show="isReadonly">保存
            </button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <div class="modal fade" id="modal-default3">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">确认入库</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal max_height">
              <div class="clearfix">
                <div class="form-group">
                  <div class="col-sm-4 flex">
                    <label class="control-label bg-gray min-width">入库单编号：</label>
                    <label class="control-label">{{acknowledgement.whInVo.inNum}}</label>
                  </div>
                  <div class="col-sm-4 flex">
                    <label class="control-label bg-gray min-width">调拨单编号：</label>
                    <label class="control-label">{{acknowledgement.whInVo.inType | inTypeFilter}}</label>
                  </div>
                  <div class="col-sm-4 flex">
                    <label class="control-label bg-gray min-width">仓库位置：</label>
                    <label class="control-label">{{acknowledgement.whInVo.whName}}</label>
                  </div>
                </div>
                <div class="clearfix">
                  <div class="col-sm-12">
                    <table id="example1" class="table table-bordered marginNonde">
                      <thead>
                      <tr>
                        <th style="width: 10%;">商品图片</th>
                        <th>商品信息</th>
                        <th style="width: 10%">入库数量</th>
                        <th style="width: 19%">指定货架</th>
                        <th style="width: 100px">数量</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr v-if="acknowledgement.whInListVOS.length == 0">
                        <td colspan="5">
                          没有数据
                        </td>
                      </tr>
                      <tr v-for="(acknowledgementItem,index) in acknowledgement.whInListVOS">
                        <td>
                          <img v-if="acknowledgementItem.goodPic" :src="acknowledgementItem.goodPic" alt="">
                          <img v-else src="@/assets/noPic.png" alt="">
                        </td>
                        <td class="text-left" style="padding-left: 20px;">
                          <label class="m-l-15">
                            <p>
                              <label>商品编码：</label>
                              <span>{{acknowledgementItem.sku}}</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>{{acknowledgementItem.goodName}}</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>{{acknowledgementItem.brandName}}</span>
                            </p>
                          </label>
                        </td>
                        <td>{{acknowledgementItem.showSendQty}}</td>
                        <td>
                          <div class="pull-left">
                            <ul style="margin: 0;">
                              <li style="margin: 3px 0;"
                                  v-for="(acknowledgementItemShelf,indexLn) in acknowledgementItem.stockVOs">
                                <div class="input-group">
                                  <select name="" style="width: 100px" class="form-control"
                                          v-model="acknowledgementItemShelf.stockId">
                                    <option v-for="storagePositionSaveItem in acknowledgementItem.storageList"
                                            :value="storagePositionSaveItem.id">
                                      {{storagePositionSaveItem.stockNo}}
                                    </option>
                                  </select>
                                  <span class="input-group-addon curren" @click="deleteShelf(index,indexLn)">
                                    <i class="fa fa-remove"></i>
                                  </span>
                                  <i class="fa fa-plus pull-right addPos"
                                     @click="addShelf(acknowledgementItem.storageList.length,index,indexLn)"></i>
                                </div>
                              </li>
                            </ul>
                          </div>
                        </td>
                        <td>
                          <p style="margin: 3px 0;" v-for="acknowledgementItemShelf in acknowledgementItem.stockVOs">
                            <input type="text" class="form-control"
                                   style="width: 80px;display: inline-block;"
                                   v-model="acknowledgementItemShelf.sendQty">
                          </p>
                        </td>
                      </tr>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" @click="okOhInListVOSFn">保存</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <div class="modal fade" id="modal-default4">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">选择商品</h4>
          </div>
          <div class="modal-body">
            <div class="form-horizontal">
              <div class="clearfix">
                <div class="clearfix">
                  <div class="col-sm-12 clearfix flex">
                    <label class="control-label bg-gray min-width">输入搜索：</label>
                    <input type="text" class="form-control pull-left" style="width: 350px" v-model="chooseProParmas.sku"
                           placeholder="请输入货号">
                    <button class="btn bg-green m-lr-10 pull-left" @click="searchProduct">搜&nbsp;索</button>
                    <button class="btn btn-danger m-lr-10 pull-left" @click="resetProduct">重&nbsp;置</button>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-12">
                    <!--<button type="button" class="btn btn-success pull-left m-r-10" data-toggle="modal" data-target="#modal-default">创建调拨单</button>-->
                  </label>
                </div>
                <div class="clearfix">
                  <div class="col-sm-12">
                    <table id="example5" class="table table-bordered table-hover marginNonde">
                      <thead>
                      <tr>
                        <th>
                          <label>
                            <!--<input type="checkbox" class="flat-red">-->
                          </label>
                        </th>
                        <th>商品信息</th>
                      </tr>
                      </thead>
                      <tbody>
                      <tr v-if="addProductList.length == 0">
                        <td colspan="2"></td>
                      </tr>
                      <tr v-else v-for="(addProductListItem,index) in addProductList">
                        <td>
                          <label>
                            <input type="checkbox" class="flat-red" :value="addProductListItem"
                                   v-model="checkedProduct">
                          </label>
                        </td>
                        <td class="text-left">
                          <img v-if="addProductListItem.goodsPic" :src="addProductListItem.goodsPic" alt="">
                          <img v-else src="@/assets/noPic.png" alt="">
                          <label class="m-l-15">
                            <p>
                              <label>商品编码：</label>
                              <span>{{addProductListItem.sku}}</span>
                            </p>
                            <p>
                              <label>商品名称：</label>
                              <span>{{addProductListItem.goodsCn}}</span>
                            </p>
                            <p>
                              <label>品牌名称：</label>
                              <span>{{addProductListItem.brandName}}</span>
                            </p>
                          </label>
                        </td>
                      </tr>
                      </tbody>
                    </table>
                    <div class="clearfix" v-if="addProductList.length != 0">
                      <div class="m-t-15 pull-right">
                        <pagination :total="pages.productList.total" :current-page='pages.productList.pageNum'
                                    :display="pages.productList.display" @pagechange="pagechange1"></pagination>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button"  class="btn btn-primary" @click="addProductSubmit">保存
            </button>
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
            </button>
          </div>
        </div>
        <!-- /.modal-content -->
      </div>
      <!-- /.modal-dialog -->
    </div>
    <!--<div id="box">-->
    <!--<transition name="fade">-->
    <!--<div class="error" v-show="isShow" transiton="fade">-->
    <!--<p>{{message}}</p>-->
    <!--</div>-->
    <!--</transition>-->
    <!--</div>-->
  </section>
</template>

<script>
  import stockManagement from '@/js/storage/stockManagement'
  import instance from '@/api/index.js'
  import pagination from '@/components/common/pageComponent'

  export default {
    name: "stockManagement",
    data() {
      return {
        isShow: true,//显示错误提示框
        storeListmap: new Map(),//显示错误提示框
        message: "",//错误信息
        activeInStorageWh: -1,//仓库位置addClass样式
        inStorageState: [{name: '全部'}, {name: '待入库', status: "1"}, {name: '已入库', status: "2"}, {
          name: '已取消',
          status: "3"
        }],//入库状态
        activeInStorageState: 0,//入库状态addClass样式
        getInStorageList: [],//获取入库列表
        show: false,//loading加载
        checked: false,
        isReadonly: false,//设置输入框只读
        checkList: [],//全选/反选获取的id
        getInStorageListParam: {},//获取列表数据传递参数
        inStorageListDetails: [],//获取详情列表行数据

        editorGetInStorage: {
          whInVo: {
            inNum: null,
            inType: '',
            whId: '',
          },
          whInListVOS: []
        },//编辑获取数据
        userPrivilegeStock: [],//获取用户可查看仓库
        shelfMap: new Map(),
        pages: {
          inStorckList: {
            total: 0,
            pageNum: 1,
            display: 10
          },
          productList: {
            total: 0,
            pageNum: 1,
            display: 5
          },
          inventory: {
            total: 0,
            pageNum: 1,
            dispaly: 5
          }
        },//分页
        analysisType: {
          inStorck: [
            {name: "生产入库", id: '1'},
            {name: "调拨入库", id: '2'},
            {name: "采购入库", id: '3'},
            {name: "退货入库", id: '4'},
            {name: "其他入库", id: '5'}
          ],
          inStorckStatus: ["已入库", "待入库"]
        },//解析状态码
        inStorckInNum: false,//创建和编辑传值
        selectedStorck: {//下拉框选择
          inStorckType: "",//入库类型
          inStorckWarehouse: "",//仓库
        },
        submitInStorckParam: {},//创建和编辑返回后台数据
        InStorckLists: [],//调拨单明细对象
        chooseWarehouseLists: [],//选择仓库下拉列表
        accessToLocationLists: [],//获取库位下拉列表
        arr: [],
        addProductList: [],//添加产品获取库存列表
        checkedProduct: [],//选择产品checkbox
        checkedProductList: [],//选择产品列表
        saveCheckedProductList: [],//存储选择产品列表
        storagePositionSave: [],//存放库位信息

        showShelf: false,//
        readonly: false,//只读
        acknowledgement: {
          whInVo: {
            inNum: null,
            inType: null,
            whName: ""
          },
          whInListVOS: {}
        },//确认入库

        okOhInListVOS: {},//确认入库提交
        chooseInWh: '',
        addProductP: true,//添加产品
        modalInfo: '',//创建编辑详情弹框提示
        alertInfo: '',//错误信息提示
        foreachIndex: 0,//递归下标
        storageMap: new Map(),
        storageObj: {},
        storageList: [],
        chooseProParmas: {},
      }
    },
    methods: {
      //消息弹窗提示
      alertMessage: function (value) {
        let type = 'success'
        if (!value.success) {
          type = 'error'
        }
        this.$message({
          showClose: false,
          message: value.msg,
          type: type
        });
      },
      // 获取分页
      pagechange: function (currentPage) {
        this.pages.inStorckList.pageNum = currentPage;
        this.getInStorageListParam.pages = currentPage;
        this.getInStorageListFn();
      },

      pagechange1: function (currentPage) {
        this.pages.productList.pageNum = currentPage;
        this.addProduct();
      },

      //获取入库列表
      getInStorageListFn: function () {
        const vm = this;
        vm.show = true;
        vm.getInStorageListParam.page = vm.pages.inStorckList.pageNum;
        vm.getInStorageListParam.rows = 20;
        vm.pages.inStorckList.display = vm.getInStorageListParam.rows;
        console.log(vm.getInStorageListParam)
        instance.post("/v1/whReceiptProcess/getWareHouserInList", vm.getInStorageListParam).then(function (data) {
          vm.getInStorageList = data.data.data.list;
          vm.pages.inStorckList.total = data.data.data.total;
          console.log(data)
          vm.show = false;//loading加载
        }).catch(function (err) {
          console.log(err);
        })
      },


      // 获取用户可查看仓库
      userPrivilegeStockFn: function () {
        const vm = this;
        instance.post('/v1/warehouse/getUserWareHouseList').then(function (data) {
          vm.userPrivilegeStock = data.data.data;
        })
      },

      //取消入库单
      outCancel: function (id) {
        let vm = this;
        vm.$confirm('此操作将取消此入库单, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          instance.post('/v1/whReceiptProcess/cancelWareHouserIn/' + id).then(function (data) {
            vm.alertMessage(data.data);
            vm.getInStorageListFn();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
        // instance.post('/v1/whReceiptProcess/deleteWareHouserIn/'+ id).then(function (data) {
        //   vm.getInStorageListFn();
        //   console.log(data)
        // })
      },

      //仓库位置查询
      chooseTransferWh: function (index, whId) {
        this.activeInStorageWh = index;
        this.getInStorageListParam.whId = whId;
        this.getInStorageListParam.inNum = null;
        this.getInStorageListFn();
      },

      //入库状态查询
      chooseTransferStatus: function (index, status) {
        this.activeInStorageState = index;
        this.getInStorageListParam.status = status;
        this.getInStorageListParam.inNum = null;
        this.getInStorageListFn();
      },

      //输入搜索条件
      searchAllocationNum: function (e) {
        this.getInStorageListParam.status = null;
        this.getInStorageListParam.whId = null;
        this.getInStorageListFn();
      },

      //重置查询
      resetCondition: function () {
        for (let key in this.getInStorageListParam) {
          this.getInStorageListParam[key] = null;
        }
        this.activeInStorageWh = -1;
        this.activeInStorageState = 0;
        this.getInStorageListFn();
      },

      //创建入库单
      addInStorck: function () {
        let vm = this;
        vm.showShelf = false;
        vm.inStorckInNum = false;
        vm.isReadonly = true;
        this.modalInfo = "创建入库单";
        this.getInStorageListParam.inNum = null;
        for (let key in vm.editorGetInStorage.whInVo) {
          vm.editorGetInStorage.whInVo[key] = "";
        }
        vm.editorGetInStorage.whInListVOS = [];
      },

      //编辑入库单
      editorInStorage: function (inNum) {
        let vm = this;
        vm.showShelf = false;
        this.getInStorageListParam.inNum = null;
        this.modalInfo = "编辑入库单";
        vm.inStorckInNum = true;
        vm.isReadonly = true;
        instance.post('/v1/whReceiptProcess/getWareHouserIn', {inNum: inNum}).then(function (data) {
          vm.editorGetInStorage.whInVo = data.data.data.whInVo;
          vm.editorGetInStorage.whInListVOS = data.data.data.whInListVOS;
          for (let i = 0; i < vm.editorGetInStorage.whInListVOS.length; i++) {
            let stockQtyItem = vm.editorGetInStorage.whInListVOS[i];
            let stockQtyCount = 0;
            for (let j = 0; j < stockQtyItem.stockVOs.length; j++) {
              if (stockQtyItem.stockVOs[j].stockQty) {
                stockQtyCount = stockQtyCount + parseInt(stockQtyItem.stockVOs[j].stockQty);
              }
            }
            stockQtyItem.allStockQty = stockQtyCount;
          }
          console.log(data.data.data)
        });
      },

      detailInStorage: function (inNum, status) {
        let vm = this;
        if (status == '2') {
          vm.showShelf = true;
        }
        this.getInStorageListParam.inNum = null;
        this.modalInfo = "入库单详情";
        vm.inStorckInNum = true;
        vm.isReadonly = false;
        instance.post('/v1/whReceiptProcess/getWareHouserIn', {inNum: inNum}).then(function (data) {
          vm.editorGetInStorage.whInVo = data.data.data.whInVo;
          vm.editorGetInStorage.whInListVOS = data.data.data.whInListVOS;
          // vm.chooseWarehouse(vm.editorGetInStorage.whInVo.whId);
        });
      },

      //确认入库
      acknowledgementFn: function (inNum) {
        let vm = this;
        vm.showShelf = false;
        vm.foreachIndex = 0;
        vm.storeListmap = new Map();
        instance.post("/v1/whReceiptProcess/getWareHouserIn", {inNum: inNum}).then(function (data) {
          vm.acknowledgement.whInVo = data.data.data.whInVo;
          for (let i = 0; i < data.data.data.whInListVOS.length; i++) {
            let currentItem = data.data.data.whInListVOS[i];
            let skuCount = 0;
            for (let j = 0; j < currentItem.stockVOs.length; j++) {
              skuCount = skuCount + parseInt(currentItem.stockVOs[j].sendQty);
            }
            currentItem.showSendQty = skuCount;
          }
          vm.selectStorage(vm.acknowledgement.whInVo.whId, data.data.data.whInListVOS);
          console.log(data.data.data)
        })
      },
      //获取库位下拉单
      selectStorage: function (id, list) {
        let vm = this;
        instance.post("/v1/warehouse/getWareHouseStockListIn", {
          whId: id,
          goodsId: list[vm.foreachIndex].goodId
        }).then(function (data) {
          list[vm.foreachIndex].selectSize = 1;
          list[vm.foreachIndex].storageList = data.data.data;
          vm.foreachIndex++;
          if (list[vm.foreachIndex]) {
            vm.selectStorage(id, list);
          } else {
            vm.acknowledgement.whInListVOS = list;
          }
        });
      },
      //搜索产品
      searchProduct: function () {
        const vm = this;
        this.checkedProduct = [];
        if (!vm.editorGetInStorage.whInVo.whId) {
          this.$message({
            showClose: true,
            message: '请先选择仓库!',
            type: 'warning'
          });
          return;
        }
        vm.chooseProParmas.whId = vm.editorGetInStorage.whInVo.whId;
        vm.chooseProParmas.page = vm.pages.productList.pageNum;
        vm.chooseProParmas.rows = vm.pages.productList.display;
        instance({
          method: 'post',
          url: '/v1/goods/getList',
          params: vm.chooseProParmas
        }).then(function (data) {
          vm.addProductList = data.data.data.list;
          vm.pages.productList.total = data.data.data.total;
          console.log(data.data.data.list)
        })
      },

      //重置
      resetProduct: function () {
        this.chooseProParmas.sku = '';
        const vm = this;
        this.checkedProduct = [];
        if (!vm.editorGetInStorage.whInVo.whId) {
          this.$message({
            showClose: true,
            message: '请先选择仓库!',
            type: 'warning'
          });
          return;
        }
        vm.chooseProParmas.whId = vm.editorGetInStorage.whInVo.whId;
        vm.chooseProParmas.page = vm.pages.productList.pageNum;
        vm.chooseProParmas.rows = vm.pages.productList.display;
        instance({
          method: 'post',
          url: '/v1/goods/getList',
          params: vm.chooseProParmas
        }).then(function (data) {
          vm.addProductList = data.data.data.list;
          vm.pages.productList.total = data.data.data.total;
          console.log(data.data.data.list)
        })
      },


      //添加货架
      addShelf: function (size, index, _index) {
        let vm = this;
        let i = 1;
        let stockVo = {
          "stockNo": null,
          "stockId": null,
          "stockQty": null,
          "sendQty": 0
        }
        let whInListVO = Object.assign({}, vm.acknowledgement.whInListVOS[index]);
        if (whInListVO.stockVOs.length >= size) {
          return;
        } else {
          _index++;
        }
        whInListVO.stockVOs.splice(_index, 0, stockVo);
        vm.$set(vm.acknowledgement.whInListVOS, index, whInListVO)
      },

      //删除货架
      deleteShelf: function (index, _index) {
        let vm = this;
        let whInListVO = Object.assign({}, vm.acknowledgement.whInListVOS[index]);
        if (_index == 0) {
          return;
        }
        whInListVO.stockVOs.splice(_index, 1);
        vm.$set(vm.acknowledgement.whInListVOS, index, whInListVO)
      },

      //确认入库提交
      okOhInListVOSFn: function () {

        let vm = this;
        let totalQty = 0;
        vm.okOhInListVOS.whInList = [];
        let stockArray = [];
        for (let i = 0; i < vm.acknowledgement.whInListVOS.length; i++) {
          let count = 0;
          for (let j = 0; j < vm.acknowledgement.whInListVOS[i].stockVOs.length; j++) {
            let okOhInListVOSObg = {};
            stockArray.push(vm.acknowledgement.whInListVOS[i].stockVOs[j].stockId);
            okOhInListVOSObg.goodId = vm.acknowledgement.whInListVOS[i].goodId;
            okOhInListVOSObg.stockId = vm.acknowledgement.whInListVOS[i].stockVOs[j].stockId;
            okOhInListVOSObg.qty = vm.acknowledgement.whInListVOS[i].stockVOs[j].sendQty;
            count = Number(count) + Number(vm.acknowledgement.whInListVOS[i].stockVOs[j].sendQty);
            if (!okOhInListVOSObg.stockId) {
              vm.$alert('请输入入库库位！', '提示', {
                confirmButtonText: '确定',
              });
              return;
            }
            if (!okOhInListVOSObg.qty) {
              vm.$alert('请输入入库数量！', '提示', {
                confirmButtonText: '确定',
              });
              return;
            }
            for (let k = 0; k < vm.acknowledgement.whInListVOS[i].storageList.length; k++) {
              let element = vm.acknowledgement.whInListVOS[i].storageList[k];
              if (element.id == okOhInListVOSObg.stockId) {
                okOhInListVOSObg.stockNo = element.stockNo;
              }
            }
            totalQty = totalQty + parseInt(okOhInListVOSObg.qty);
            vm.okOhInListVOS.whInList.push(okOhInListVOSObg)
          }
          if (count != Number(vm.acknowledgement.whInListVOS[i].showSendQty)) {
            vm.$alert('入库数量输入不正确！', '提示', {
              confirmButtonText: '确定',
            });
            return;
          }
        }
        //检验全部库位是否有重复的
        // let anies = Array.from(new Set(stockArray));
        // if (anies.length != stockArray.length) {
        //   vm.$alert('不同商品sku，入库库位不能相同！', '提示', {
        //     confirmButtonText: '确定',
        //   });
        //   return;
        // }
        vm.okOhInListVOS.whIn = {
          "inNum": vm.acknowledgement.whInVo.inNum,
          "whId": vm.acknowledgement.whInVo.whId,
          "inType": vm.acknowledgement.whInVo.inType,
          "inVoucher": vm.acknowledgement.whInVo.inVoucher,
          "status": 2,
          "delFlage": "0",
          "total": totalQty,
          "note": null
        };
        instance.post('/v1/whReceiptProcess/okWareHouserIn', vm.okOhInListVOS).then(function (data) {
          // if(data.data.success){
          //   vm.getInStorageListFn();
          // }
          $("#modal-default3").modal('hide');
          vm.alertMessage(data.data);
          vm.getInStorageListFn();
          console.log(data)
        })

      },


      //删除入库单详情单行数据
      deleteInStorck: function (index) {
        this.getInStorageListParam.inNum = null;
        this.editorGetInStorage.whInListVOS.splice(index, 1);
      },

      // 提交入库单和创建入库单
      submitInStorck: function () {
        let vm = this;
        let totalQty = 0;
        if (!vm.editorGetInStorage.whInVo.inType) {
          vm.$alert('请选择入库类型！', '提示', {
            confirmButtonText: '确定',
          });
          return;
        }
        vm.InStorckLists = [];
        for (let i = 0; i < vm.editorGetInStorage.whInListVOS.length; i++) {
          let transferAdd = {};
          for (let j = 0; j < vm.editorGetInStorage.whInListVOS[i].stockVOs.length; j++) {
            transferAdd.goodId = vm.editorGetInStorage.whInListVOS[i].goodId;
            transferAdd.qty = vm.editorGetInStorage.whInListVOS[i].stockVOs[j].sendQty;
            if (!transferAdd.qty || transferAdd.qty <= 0) {
              vm.$alert('请输入正确的入库数量！', '提示', {
                confirmButtonText: '确定',
              });
              return;
            }
            totalQty = totalQty + parseInt(transferAdd.qty);
            vm.InStorckLists.push(transferAdd);
          }
        }
        vm.submitInStorckParam.whIn = {
          "whId": vm.editorGetInStorage.whInVo.whId,
          "inType": vm.editorGetInStorage.whInVo.inType,
          "inNum": vm.editorGetInStorage.whInVo.inNum,
          "status": 1,
          "total": totalQty,
        };
        vm.submitInStorckParam.whInList = vm.InStorckLists;
        vm.checkInfo();
        let postUrl = "";
        if (vm.inStorckInNum) {
          postUrl = '/v1/whReceiptProcess/updateWareHouserIn';
        } else {
          postUrl = '/v1/whReceiptProcess/addWareHouserIn';
        }
        ;
        instance.post(postUrl, vm.submitInStorckParam).then(function (data) {
          console.log(data)
          // if (data.data.success) {
          //   vm.getInStorageListFn();
          // }
          $("#modal-default2").modal('hide');
          vm.alertMessage(data.data);
          vm.getInStorageListFn();
        })

      },

      //验证信息
      checkInfo: function () {
        let vm = this;
        let verify = {};
        //选择仓库
        if (vm.editorGetInStorage.whInVo.whId) {
          verify.whId = true;
        } else {
          verify.whId = false;
          vm.isShow = true;
          vm.message = "请选择仓库";
        }
        //选择入库类型
        if (vm.editorGetInStorage.whInVo.inType) {
          verify.inType = true;
        } else {
          verify.inType = false;
          vm.isShow = true;
          vm.message = "请选择入库类型";
        }

        for (let i = 0; i < vm.editorGetInStorage.whInListVOS.length; i++) {
          let current = vm.editorGetInStorage.whInListVOS[i];
          for (let j = 0; j < current.stockVOs.length; j++) {
            if (current.stockVOs[j].sendQty && current.stockVOs[j].sendQty <= current.allStockQty) {
              verify.sendQty = true;
            } else {
              verify.sendQty = false;
            }
          }
        }
      },

      //选择仓库下拉列表
      chooseWarehouse: function (whId) {
        let vm = this;
        instance.post('/v1/warehouse/getShelfListByWhId/' + whId).then(function (data) {
          vm.chooseWarehouseLists = data.data.data;
          vm.storagePositionSave = [];
        });
      },


      //添加产品
      addProduct: function () {
        this.chooseProParmas.sku = '';
        const vm = this;
        this.checkedProduct = [];
        if (!vm.editorGetInStorage.whInVo.whId) {
          this.$message({
            showClose: true,
            message: '请先选择仓库!',
            type: 'warning'
          });
          return;
        }
        vm.chooseProParmas.whId = vm.editorGetInStorage.whInVo.whId;
        vm.chooseProParmas.page = vm.pages.productList.pageNum;
        vm.chooseProParmas.rows = vm.pages.productList.display;
        instance({
          method: 'post',
          url: '/v1/goods/getList',
          params: vm.chooseProParmas
        }).then(function (data) {
          vm.addProductList = data.data.data.list;
          vm.pages.productList.total = data.data.data.total;
          console.log(data.data.data.list)
        })
      },

      //添加产品提交
      addProductSubmit: function () {
        if(this.checkedProduct.length == 0){
          this.$alert('至少选择一个商品！', '提示', {
            confirmButtonText: '确定',
          });
          return;
        }
        for (let i = 0; i < this.checkedProduct.length; i++) {
          let checkItem = {}
          checkItem.goodId = this.checkedProduct[i].id;
          checkItem.goodPic = this.checkedProduct[i].goodsPic;
          checkItem.goodName = this.checkedProduct[i].goodsCn;
          checkItem.sku = this.checkedProduct[i].sku;
          checkItem.brandName = this.checkedProduct[i].brandName;
          if (this.checkedProduct[i].stockQty) {
            checkItem.countStock = this.checkedProduct[i].stockQty;
          } else {
            checkItem.countStock = 0;
          }
          checkItem.stockVOs = [];
          let stockVO = {};
          stockVO.sendQty = 0;
          checkItem.stockVOs.push(stockVO);
          let matchTag = true;
          for (let j = 0; j < this.editorGetInStorage.whInListVOS.length; j++) {
            let currentItem = this.editorGetInStorage.whInListVOS[j];
            if (this.checkedProduct[i].id == currentItem.goodId) {
              matchTag = false;
              break;
            }
          }
          if (matchTag) {
            this.editorGetInStorage.whInListVOS.push(checkItem);
          }
        }
        $("#modal-default4").modal('hide');
        console.log(this.editorGetInStorage.whInListVOS)
      },

      //获取用户可查看仓库
      getUserWareHouseListFn: function () {
        const vm = this;
        instance.post('/v1/warehouse/getUserWareHouseList/').then(function (data) {
          vm.userPrivilegeStock = data.data.data;
          console.log(data.data.data)
        })
      },

      //对比入库数量和库存
      maxSendQty: function () {
        let lengthedWhVOS = this.editorGetInStorage.whInListVOS.length;
        for (let i = 0; i < lengthedWhVOS; i++) {
          let stockQty = this.editorGetInStorage.whInListVOS[i].stockQty;
          let current = this.editorGetInStorage.whInListVOS[i];
          for (let j = 0; j < current.stockVOs.length; j++) {

          }
        }
      }
    },
    created() {
      this.getInStorageListFn();
      //获取用户可查看仓库
      this.getUserWareHouseListFn(1);
    },
    components: {
      pagination,
    },
    filters: {
      inTypeFilter: function (value) {
        let inTypeMap = new Map();
        let returnInType = "";
        inTypeMap.set("1", "生产入库");
        inTypeMap.set("2", "调拨入库");
        inTypeMap.set("3", "采购入库 ");
        inTypeMap.set("4", "退货入库");
        inTypeMap.set("5", "其他入库");

        switch (value) {
          case "1":
            returnInType = inTypeMap.get("1");
            break;
          case "2":
            returnInType = inTypeMap.get("2");
            break;
          case "3":
            returnInType = inTypeMap.get("3");
            break;
          case "4":
            returnInType = inTypeMap.get("4");
            break;
          case "5":
            returnInType = inTypeMap.get("5");
            break;
        }
        return returnInType
      },


    }
  }


</script>

<style scoped>
</style>
